import React, { forwardRef, useState, useImperativeHandle, useRef, useEffect } from "react";
import { Menu, Space } from '@bytedance/mona-ui';
import AddCoupon from './addCoupon'
import SetCoupon from './setCoupon'
import AutoSetCoupon from './AutoSetCoupon'
import { getBgCoupOuterTuis, addNewConter, loginShopGetShopInfos } from './autoHome/autoCoupFunc'
import { useAppDispatch } from "~store"
import { getBuyCounpStatusAction } from "~store/controller/autoCounps"
function MenuBox(props, ref) {
    const [current, setcurrent] = useState(props.actstr || 'addCoupon')
    // const [current, setcurrent] = useState('AutoSetCoupon')
    const dispatch = useAppDispatch()
    type ChildMethods = {
        // 子组件暴露给父组件的方法
        someMethod: () => void;
    };
    const childRef = useRef<ChildMethods>(null);
    function someMethod() {
        if (childRef.current) {
            childRef.current?.someMethod();
        }
    }
    useImperativeHandle(ref, () => ({
        someMethod,
    }));
    useEffect(() => {
        if (!props || !props.setFooterStatus) return
        if (current === 'addCoupon') {
            props.setFooterStatus(true)
        } else {
            props.setFooterStatus(false)
        }
    }, [current])
    addNewConter()
    return (<Space direction="vertical" style={{
        width: '100%'
    }} size={18}>
        <Menu onClick={(e) => {
            console.log('e', e)
            if(e.key === 'AutoSetCoupon') {
                getBgCoupOuterTuis('')
                // addNewConter()
                loginShopGetShopInfos()
                dispatch(getBuyCounpStatusAction(''))
            }
            setcurrent(e.key)
        }} selectedKeys={[current]} mode="horizontal">
            <Menu.Item key="addCoupon">
                设置优惠券
            </Menu.Item>
            <Menu.Item key="setCoupon">
                管理优惠券
            </Menu.Item>
            <Menu.Item key="AutoSetCoupon">
                自动设置优惠券
            </Menu.Item>
        </Menu>
        {current === 'addCoupon' && <AddCoupon ref={childRef} setcurrent={setcurrent} clearBox={props.clearBox} />}
        {current === 'AutoSetCoupon' && <AutoSetCoupon ref={childRef} setcurrent={setcurrent} clearBox={props.clearBox} />}
        {current === 'setCoupon' && <SetCoupon />}
    </Space>)
}
export default forwardRef(MenuBox)